package com.sdu.game.wbr.game_play;

import com.sdu.game.wbr.role_script.Player;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Vote {

    public List<Integer> players;

    /** 投票信息表 */
    public Map<Integer,Integer> VoteInfoMap = new HashMap<>();

    public Map<Integer,Integer> VoteResultdMap = new HashMap<>();

//    public Map<Player, Integer> VoteMap;

//    public List<Integer> results;
    public int[] results;

    public Vote(){
        players = new ArrayList<Integer>();
    }

    public Vote(List<Player> playerList){

//        for(Player p : playerList){
//            VoteMap.put(p,0);
//        }

//        results = new ArrayList<Integer>();
//
//        for( Player p : playerList){
//            players.add(p);
//            results.add(0);
//        }

        players = new ArrayList<Integer>();
//        players.addAll(playerList);

        results = new int[playerList.size()];

    }

    /**
     * 投票
     * @param player
     * @param voted_player
     * @return
     */
    public boolean VotePlayer(int player, int voted_player){
//        if(!player.alive){
//            return false;
//        }

        if(!players.contains(player)){
            players.add(player);
        }

        VoteInfoMap.put(player,voted_player);

        if(VoteResultdMap.containsKey(voted_player)){
            int now_res = VoteResultdMap.get(voted_player);
            VoteResultdMap.put(voted_player,++now_res);
        }
        else{
            VoteResultdMap.put(voted_player,1);
        }

//        results[players.indexOf(player)]++;
//        results.set(players.indexOf(player),results.get(players.indexOf(player)+1));
//        VoteMap.put(player, VoteMap.get(player)+1);
        return true;
    }

    /**
     * 检查投票结果，返回被选出的Player对象
     * @return
     */
    public int CheckResults(){

        List<Integer> MaxIndexList = getIndexOfMax();
        if(MaxIndexList.size() == 1){
            // 杀死玩家
            return MaxIndexList.get(0);
        }
        else{
            // 重新投票

            return 0;
        }

    }


    private List<Integer> getIndexOfMax(){
//        int cur = 0, max_cur = 0;
        int max = 0;

        List<Integer> IndexList = new ArrayList<Integer>();

//        System.out.println("Checking!");

        for(int i : VoteResultdMap.keySet()){
            if(VoteResultdMap.get(i) > max){
//                System.out.println(i + " Max Replace !" + VoteResultdMap.get(i));
                IndexList.clear();
                IndexList.add(i);
                max = VoteResultdMap.get(i);
            }
            else if(VoteResultdMap.get(i) == max){
                IndexList.add(i);
            }
        }

//        for(int i=0; i<results.length; i++){
//            if(results[i] > max){
//                IndexList.clear();
//                IndexList.add(i);
//                max = results[i];
//            }
//            else if(results[i] == max){
//                IndexList.add(i);
//            }
//        }

        return IndexList;

    }

}
